/**注册第一步*/
$(function(){
	
	var cookie = new sweetCookie();
	var email=cookie.getCookie("email");
	if(email==null){
		
		location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/login/E-mail.html";
	}
	$("#repeatAlert").hide();
	var checkboxVal;
	$("#codePat").hide();
	//密码格式验证
	function codepat(){
		var codeVal=$("#password").val();
		var patCode=/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,16}$/;
		if(patCode.test(codeVal)){
			return true;
		}else{
			return false;
		}
	};
	function codepat2(){
		var codeVal=$("#password").val();
		var patNone=/[\s]/;
		if(patNone.test(codeVal)){
			return true;
		}else{
			return false;
		}
	};
	//用户名
	function usernamepat(){
		var usernameVal=$("#name").val();
		var patUsername=/^[a-zA-Z0-9_\u4e00-\u9fa5]{5,25}$/;
		if(patUsername.test(usernameVal)){
			return true;
		}else{
			return false;
		}
	};
	$("#userNamePat").hide();
	$("#name").on("blur",function(){
		var result=usernamepat();
		if(!$("#name").val()){
			$("#userNamePat").html("*必填").css("right","0").show();
			$("#repeatAlert").hide();
		}else if(result){
			$("#userNamePat").hide();
		}else{
			$("#userNamePat").html("*可由中英文、数字、下划线组成5-25个字符").css("right","-238px").show();
		}
	});
	$("#password").on("blur",function(){
		var result=codepat();
		var result2=codepat2();
		if(!$("#password").val()){
			$("#codePat").html("*必填").css("right","0").show();
		}else if(result  && !result2){
			$("#codePat").hide();
		}else{
			$("#codePat").html("*6-16位字母、数字和符号(除空格),至少含2种").css("right","-242px").show();
		}
	});
	$("#passwordagain").on("blur",function(){
		var passWordAgain=$("#passwordagain").val();
		var passWord=$("#password").val();
		if(!passWordAgain){
			$(".secondpass").html("*必填").css("right","0").show();
		}else if(passWordAgain!=passWord){
			$(".secondpass").html("*请保证密码一致").css("right","-67px").show();
		}else{
			$(".secondpass").hide();
		}
	});
	$("body").on("mousemove",function(){
		var userName=$("#name").val();
		
		checkboxVal=$("#checkBox").prop("checked");
		if(usernamepat() && checkboxVal && codepat() && userName && $("#passwordagain").val()==$("#password").val()){
			$(".step1SubmitBtn").attr("disabled",false);
		}else{
			$(".step1SubmitBtn").attr("disabled",true);
		}
	});
	$(".step1SubmitBtn").click(function(){
		doregister(); 
	});
})
function doregister() {
	var cookie = new sweetCookie();
	var advid = cookie.getCookie("advid");
	/*alert(registerAdvId)*/
	if(advid==null){
		advid = "one";
	}
	$.ajax({
		url : "/Lvscore_Advertiser_Maven/ajaxRegisterPageOne?registerAdvId="+advid,
		type : "post",
		dataType : "json",
		data : $("#adform").serialize(),
		success : function(data) {
			var result = data.error;
			if(result=="此会员昵称已注册"){
				$("#repeatAlert").show();
			}else if(result=="请返回上一步注册邮箱"){
				alert("请返回上一步注册邮箱");
			}else{
				$("#repeatAlert").hide();
				var registerReturnBean = data.registerReturnBean;
				var donkeyNum=registerReturnBean.donkeyNum;
				var name=registerReturnBean.name;
				var advid=registerReturnBean.advId;
				var email=registerReturnBean.email;
				 var sweetcookie = new sweetCookie();
                 sweetcookie.delCookie("donkeyNum");
                 sweetcookie.setCookie("donkeyNum", donkeyNum);
                 sweetcookie.delCookie("name");
                 sweetcookie.setCookie("name", name);
                 sweetcookie.delCookie("advId");
                 sweetcookie.setCookie("advid", advid);
				location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/login/registerStep2.html";
			}
		}
	});
};
var newCookie = new sweetCookie();
var getUserAgree = function(){
	var agree = newCookie.getCookie("userAgree");	
	if(agree){
	   clearInterval(setGetUserAgree);
	   newCookie.delCookie("userAgree");
	   $("#checkBox")[0].checked = true;
	};
};
var setGetUserAgree = setInterval(getUserAgree,50);
